#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import rospy
from std_msgs.msg import String
from geometry_msgs.msg import Twist
from ros_demo.msg import CustomMessage

def chatter_callback(msg):
    """处理字符串消息的回调函数"""
    rospy.loginfo(f"收到消息: {msg.data}")

def cmd_vel_callback(msg):
    """处理速度控制消息的回调函数"""
    rospy.loginfo(f"收到速度命令 - 线速度: {msg.linear.x:.2f}, 角速度: {msg.angular.z:.2f}")

def custom_callback(msg):
    """处理自定义消息的回调函数"""
    rospy.loginfo(f"收到自定义消息 - 名称: {msg.name}, ID: {msg.id}, 值: {msg.value:.2f}, 位置: ({msg.position.x:.2f}, {msg.position.y:.2f}, {msg.position.z:.2f})")

def listener():
    # 初始化ROS节点
    rospy.init_node('listener_py', anonymous=True)
    
    # 创建订阅者
    rospy.Subscriber('chatter', String, chatter_callback)
    rospy.Subscriber('cmd_vel', Twist, cmd_vel_callback)
    rospy.Subscriber('custom_topic', CustomMessage, custom_callback)
    
    rospy.loginfo("Python Listener节点启动，开始监听消息...")
    
    # 进入循环，等待回调函数
    rospy.spin()

if __name__ == '__main__':
    try:
        listener()
    except rospy.ROSInterruptException:
        pass
